<?
/*
+--------------------------------------------------------------------------
|   扩展上传类
|   =============================================
|   by Benzene(lilei)
|   http://dev.sdo.com
|   =============================================
|   Web: http://dev.sdo.com
|   Time: 17 Mar 2010 11:04:44 GMT
+--------------------------------------------------------------------------
*/
if (!defined('BASEPATH')) exit('No direct script access allowed');
class SDO_Upload extends CI_Upload
{
	var $result = array();
	public $is_flash_upload = FALSE;
	function SDO_Upload($props = array())
	{
		parent::CI_Upload($props);
	}

	function do_upload($field = 'userfile', $checkRule = array(), $diffLimit = FALSE)
	{
		$success = FALSE;
		$name = time() . rand(0, 999999);

		$baseConfig = array(
		    'allowed_types' => 'gif|jpg|png|zip|rar',
		    'max_size' => '100000',
		    'overwrite' => true,
		);

		//如果不对各表单field单独做图片限制检查
		if (!$diffLimit) {
		    $checkRule += $baseConfig;
		} else {
		    foreach ($checkRule as $fieldkey => $subConfig) {
		        $checkRule[$fieldkey] += $baseConfig;
		    }
		}

		//开始上传
		if (isset($_FILES[$field]) && is_array($_FILES[$field]['error']) && $diffLimit) {
			for ($i = 0; $i < count($_FILES[$field]['error']); $i++)
			{
				$pseudo_field_name = '_psuedo_'. $field .'_'. $i;
				$_FILES[$pseudo_field_name] = array(
					'name' => $_FILES[$field]['name'][$i],
					'size' => $_FILES[$field]['size'][$i],
					'type' => $_FILES[$field]['type'][$i],
					'tmp_name' => $_FILES[$field]['tmp_name'][$i],
					'error' => $_FILES[$field]['error'][$i]
				);

				if ($diffLimit) {
				    $configKey = 'field' . ($i + 1);
				    if (isset($checkRule[$configKey])) {
				        $config = $checkRule[$configKey];
				    } else {

				        $config = $baseConfig;
				    }
				}

				$config['id']  = $i;
				$config['file_name']  = md5($name . "-{$i}");

				//初始化资源信息
				parent::initialize($config);

				//开始上传
				$success = parent::do_upload($pseudo_field_name);
				if($success) {
					$tmp = parent::data();
					$tmp["id"] = $i;
					array_push($this->result, $tmp);
				} else {
					$tmp = array();
					if(!empty($_FILES[$field]['name'][$i])) $tmp["failed"] = true;
					$tmp["id"] = $i;
					array_push($this->result,$tmp);
				}
			}
		} else {
			//初始化资源信息
			$checkRule['file_name']  = md5($name . "-" . rand(0, 99999));
			parent::initialize($checkRule);
			$success = parent::do_upload($field);
			if($success) {
				$tmp = parent::data();
				$tmp["id"] = 0;
				array_push($this->result,$tmp);
			} else {
				$tmp = parent::data();
				$tmp["id"] = 0;
				$tmp["failed"] = true;
				array_push($this->result,$tmp);
			}
		}
		return $success;
	}
    public function initialize($config = array())
    {
        $defaults = array(
            'max_size'            => 0,
            'max_width'            => 0,
            'max_height'        => 0,
            'allowed_types'        => "",
            'file_temp'            => "",
            'file_name'            => "",
            'orig_name'            => "",
            'file_type'            => "",
            'file_size'            => "",
            'file_ext'            => "",
            'upload_path'        => "",
            'overwrite'            => FALSE,
            'encrypt_name'        => FALSE,
            'is_image'            => FALSE,
            'image_width'        => '',
            'image_height'        => '',
            'image_type'        => '',
            'image_size_str'    => '',
            'error_msg'            => array(),
            'mimes'                => array(),
            'remove_spaces'        => TRUE,
            'xss_clean'            => FALSE,
            'temp_prefix'        => "temp_file_",
            'is_flash_upload'   => FALSE   // <------------------  this parameter is new!
        );
        foreach ($defaults as $key => $val)
        {
            if (isset($config[$key]))
            {
                $method = 'set_'.$key;
                if (method_exists($this, $method))
                {
                    $this->$method($config[$key]);
                }
                else
                {
                    $this->$key = $config[$key];
                }
            }
            else
            {
                $this->$key = $val;
            }
        }
    }

	public function is_allowed_filetype()
	{
		if (count($this->allowed_types) == 0 OR ! is_array($this->allowed_types))
		{
			$this->set_error('upload_no_file_types');
			return FALSE;
		}

		$image_types = array('gif', 'jpg', 'jpeg', 'png', 'jpe');

		foreach ($this->allowed_types as $val)
		{
			$mime = $this->mimes_types(strtolower($val));
			if (is_array($mime))
			{
				if (in_array($this->file_type, $mime, TRUE))
				{
					return TRUE;
				}
			}
			else
			{
				if ($mime == $this->file_type)
				{
					return TRUE;
				}	
			}		
		}
		
		return FALSE;
	}

    public function is_image()
    {
        $png_mimes;
        $jpeg_mimes;

        if($this->is_flash_upload)
        {
            log_message('debug', 'MY_Upload->is_image(): Adding application/octet-stream as valid image-mime-type and doing some headerchecks to verify that.');

            // just added the ugly flash-mime type application/octet-stream to the array
            $png_mimes  = array('image/x-png', 'application/octet-stream');
            $jpeg_mimes = array('image/jpg', 'image/jpe', 'image/jpeg', 'image/pjpeg', 'application/octet-stream');
        }
        else
        {
            // IE will sometimes return odd mime-types during upload, so here we just standardize all
            // jpegs or pngs to the same file type.
            $png_mimes  = array('image/x-png');
            $jpeg_mimes = array('image/jpg', 'image/jpe', 'image/jpeg', 'image/pjpeg');
        }

        if (in_array($this->file_type, $png_mimes))
        {
            $this->file_type = 'image/png';
        }

        if (in_array($this->file_type, $jpeg_mimes))
        {
            $this->file_type = 'image/jpeg';
        }

        if($this->is_flash_upload)
        {
            $img_mimes = array(
                            'image/gif',
                            'image/jpeg',
                            'image/png',
                            'application/octet-stream'
                           );
        }
        else
        {
            $img_mimes = array(
                            'image/gif',
                            'image/jpeg',
                            'image/png',
                           );
        }

        return (in_array($this->file_type, $img_mimes, TRUE)) ? TRUE : FALSE;
    }

}